package com.kkb.filter;

import com.kkb.bean.User;
import com.kkb.service.CourierService;
import com.kkb.utils.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// ,"/wx/userInfo.do"
@WebFilter(urlPatterns = {"/index.html"})
public class WxUserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        System.out.println("url:"+request.getRequestURI());
        String phone = isExistCookie(request);
        System.out.println(phone);
        boolean flag = false;
        if (UserUtil.getUserInfo(request.getSession()) != null){
            // 判断是否有session值 有的话直接放行
            if (phone == null || phone.length()==0) {
                System.out.println("没有cookie值了，我们已经把session清空");
                // 没有cookie值了，我们就把session清空
                request.getSession().invalidate();
            } else {
                System.out.println("cookie/session 都有值 放行");
                // cookie 有值 放行
                flag =true;
                filterChain.doFilter(servletRequest, servletResponse);
            }
        } else {
            if (phone != null && phone.length()>0) {
                System.out.println("session没值，cookie有值");
                // 存入session
                User user = null;
                // 判断是否是快递员
                boolean exist = CourierService.isExist(phone);
                if (exist) user = new User(phone,false);
                else user = new User(phone,true);
                // 存入session
                HttpSession session = request.getSession();
                UserUtil.setUserInfo(session, user);
                User test = UserUtil.getUserInfo(session);
                flag = true;
                filterChain.doFilter(servletRequest, servletResponse);
            }

        }
        if (!flag) {
            System.out.println("被拦截到login.html");
            response.sendRedirect("/login.html");
        }
    }

    /**
     * 判断是否有存有电话号码的cookie
     * @param request
     * @return 返回cookie存入的电话号码
     */
    public String isExistCookie(HttpServletRequest request){
        // 判断是否有cookie
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if ("user_cookie".equals(cookie.getName())){
                    // 如果有值，将cookie值存入session中
                    String phone = cookie.getValue();
                    System.out.println("isExistCookie: phone:" +phone);
                    return phone;
                }
            }
        }

        return null;
    }

    @Override
    public void destroy() {
    }
}
